<?php

/**
 * HTMLタグ生成
 */
function publishHtmlTag ($tag, Array $attr = null, $class = null, $style = null)
{
    $attr_str = '';
    foreach ($attr as $k => $v) {
        $attr_str .= " $k=\"{$v}\" ";
    }
    return '<'.$tag.' '.$attr_str.' /'.'>';
}


/**
 * メンバーパネル生成
 */
function publishMemberPanel ($member_id)
{
    $etw = new etwork;
    
    //$etw->setTemplate('panel', null, null, 'member|solo'.$member_id);
    $etw->setTemplate('panel', null, null, 0);
    $etw->setTemplateGroup('member');

    if (! $etw->isCached() ) {
        $loader = new modelLoader;
        $loader->model = 'member';
        $loader->targetChoiceFunction = 'findPanelById';
        $loader->solo = $member_id;
        $etw->addLoader($loader);
        $etw->isPartContent = true;
        $etw->loadData();
    }
    $etw->showFinal();
}

function publishServiceLink ( $type, $array )
{
    switch ($type) {
        case 'hp':
            return "<a href='{$array[0]}' target='_blank'>ホームページ（{$array[0]}）</a>";
        case 'twitter':
            return "<a href='{$array[0]}' target='_blank'>Twitter（{$array[0]}）</a>";
        default:
            break;
    }
}


/**
 * メンバーアバター生成
 */
function publishMemberAvatar ($image_path, $size = 24)
{
    $src = (!$image_path) ? $src = URL_IMAGE.'noicon.jpg' : URL_IMAGE.$image_path;
    return publishHtmlTag('img', array('src'=>$src, 'width'=>$size, 'height'=>$size ));
}

/**
 * 数値データから対応したラベル情報を得る
 */
function getParameterLabel($val, $params, $labels)
{
    $params = unserialize($params);
    $labels = unserialize($labels);
    $i = 0;
    foreach ($params as $i => $x) {
        if ($x == $val) {
            return $labels[$i];
        }
    }
}

/**
 * 数値を発売日に変換する
 * @param integer num 8桁の数字
 * @param boolean tsume default false 0詰めにする
 * @param boolean english default false 英語表記
 * @return string
 */
function int2Release($num, $tsume = false)
{
    if (!$num) return false;
    $num = str_replace("-","",$num);
    switch ( $num ) {
        case '11111111': $str = "中止"; break;
        case '88888888': $str = "未定"; break;
        case '90000000': $str = "なし"; break;
        case '22221111': $str = "未定"; break;
        default:
            $numY = (!$tsume) ? substr($num, 0, 4) . "年" : '';
            switch ( substr($num, 4, 4) ) {
                case '9999': $str = $numY . "予定"; break;
                case '1277': $str = $numY . "末"; break;
                case '1266': $str = $numY . "後半"; break;
                case '0666': $str = $numY . "前半"; break;
                case '0266': $str = $numY . "初頭"; break;
                
                case '1288': $str = substr($num, 0, 4) . "年冬"; break;
                case '0288': $str = "冬"; break;
                case '0588': $str = $numY . "春"; break;
                case '0888': $str = $numY . "夏"; break;
                case '1188': $str = $numY . "秋"; break;
                
                case '0840': $str = $numY . "夏コミ"; break;
                case '1240': $str = $numY . "冬コミ"; break;
                
                case '1255': $str = $numY . "第4四半期"; break;
                case '0955': $str = $numY . "第3四半期"; break;
                case '0655': $str = $numY . "第2四半期"; break;
                case '0355': $str = $numY . "第1四半期"; break;
                
                default:
                    $numM = substr($num, 4, 2);
                    $numD = substr($num, 6, 2);
                    $str = $numY . $numM . "月";
                    switch ( $numD ) {
                        case '44': $str .= "予定"; break;
                        case '43': $str .= "下旬"; break;
                        case '42': $str .= "中旬"; break;
                        case '41': $str .= "中旬"; break;
                        default : $str .= $numD . "日"; break;
                    }
                    break;
            }
            break;
    }
    return $str;
}

/**
 * ポスグレのArray形式をPHPのArrayに変換する
 */
function pgstr2ary($src) {
    $src = preg_replace('/^\{|\}$/u', '', $src);
    $ary = preg_split('//u', $src, -1, PREG_SPLIT_NO_EMPTY);
    
    $quote = false;
    $escape = false;
    $result = array();
    $value = "";
    $setted = true;
    foreach ($ary as $item) {
        if ('"' === $item) {
            if ($quote) {
                if ($escape) {
                    $value .= $item;
                } else {
                    $quote = false;
                    $setted  = false;
                }
            } else {
                $quote = true;
            }
        } else if ("\\" === $item) {
            if ($escape) {
                $escape = false;
                $value .= $item;
            } else {
                $escape = true;
            }
        } else if (',' === $item) {
            if ($quote) {
                $value .= $item;
            } else {
                $result[] = $value;
                $value = "";
                $setted = true;
            }
        } else {
            $value .= $item;
        }
        
        // エスケープ以外の時はエスケープの解除
        if ("\\" !== $item) {
            $escape = false;
        }
    }
    if (("" !== $value) || (!$setted)) {
        $result[] = $value;
    }
    foreach ($result as $k => $v) {
        if (preg_match('/^\{|\}$/u', $v)) {
            $v = pgstr2ary($v);
            $result[$k] = $v;
        }
    }
    return $result;
}




